<?php

namespace app\service;

//用户好友服务
use app\model\UserFriend;
use extend\TcpEnum;
use server\start_text_gateway;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class UserFriendService extends BaseService
{

    /**
     * 好友列表
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function list($user_id): array
    {
        $user_friend_list = (new UserFriend())->where('user_id', $user_id)->where('state', USER_FRIEND_CONFIG['状态']['已通过'])->with('user')->select();
        return $this->returnSuccess($user_friend_list->toArray());
    }


    /**
     * 添加好友
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function add($user_id, $friend_id, $remarks = '加个好友吧'): array
    {
        //创建一个好友请求
        $user_friend = (new UserFriend())->where('user_id', $user_id)->where('friend_id', $friend_id)->find();

        if ($user_friend) {
            if ($user_friend->state == USER_FRIEND_CONFIG['状态']['已通过']) {
                return $this->returnFail('已经是好友了');
            }

            if ($user_friend->state == USER_FRIEND_CONFIG['状态']['发起请求']) {
                return $this->returnFail('已经发起过请求了');
            }

            if ($user_friend->state == USER_FRIEND_CONFIG['状态']['已拒绝']) {
                $user_friend->state = USER_FRIEND_CONFIG['状态']['发起请求'];
                $user_friend->remarks = $remarks;
                $this->push($friend_id);
                $user_friend->save();
                return $this->returnSuccess('已重新发起请求');
            }
        }

        $user_friend = new UserFriend();
        $user_friend->user_id = $user_id;
        $user_friend->friend_id = $friend_id;
        $user_friend->state = USER_FRIEND_CONFIG['状态']['发起请求'];
        $user_friend->remarks = $remarks;

        $res = $user_friend->save();
        if (!$res) {
            return $this->returnFail('请求发送失败');
        } else {
            $this->push($friend_id);
            return $this->returnSuccess('请求已发送');
        }

    }

    //验证好友
    public function verify($user_id, $friend_id, $state): array
    {
        $user_friend = (new UserFriend())->where('user_id', $user_id)->where('friend_id', $friend_id)->find();
        if (!$user_friend) {
            return $this->returnFail('好友请求不存在');
        }

        if ($user_friend->state == USER_FRIEND_CONFIG['状态']['已通过']) {
            return $this->returnFail('已经是好友了');
        }

        if ($user_friend->state == USER_FRIEND_CONFIG['状态']['已拒绝']) {
            return $this->returnFail('已经拒绝了');
        }

        if ($state == USER_FRIEND_CONFIG['状态']['已通过']) {
            $user_friend->state = USER_FRIEND_CONFIG['状态']['已通过'];
            $res = $user_friend->save();
            if (!$res) {
                return $this->returnFail('添加好友失败');
            } else {
                $this->push($friend_id);
                return $this->returnSuccess('添加好友成功');
            }
        }

        if ($state == USER_FRIEND_CONFIG['状态']['已拒绝']) {
            $user_friend->state = USER_FRIEND_CONFIG['状态']['已拒绝'];
            $res = $user_friend->save();
            if (!$res) {
                return $this->returnFail('拒绝好友失败');
            } else {
                $this->push($friend_id);
                return $this->returnSuccess('拒绝好友成功');
            }
        }

        return $this->returnFail('参数错误');

    }


    //消息推送
    public function push($friend_id): void
    {
        $data = [
            'type' => TcpEnum::ADD_FRIEND,
            'data' => [
                'msg' => '你有新的好友请求'
            ]
        ];

        $aa = start_text_gateway::getAllUidList();

        var_dump($aa);
        var_dump('推送消息' . $friend_id);

        start_text_gateway::sendToUid($friend_id, json_encode($data));
    }


}